package com.woniuxy.proxy.cglib;

import com.woniuxy.proxy.aspect.LogForProxy;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author linke
 * @version 1.0
 * @className: CglibProxy
 * @description: Cglib动态代理的实现类
 * @date 2021/11/15 15:10
 */
public class CglibProxy {

    public static Object proxy(Object targetClass){

        Object objProxy = Enhancer.create(targetClass.getClass(),targetClass.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //代理业务逻辑    方法前增强
                new LogForProxy().before();
                //目标类方法     相当于反射一个对象执行对象的方法
                Object obj = method.invoke(targetClass, args);
                //方法后增强
                new LogForProxy().after();
                return obj;
            }
        });
        return objProxy;
    }



}
